#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/6/22 22:09
import os

import pandas as pd
from os import sep

from config.config import config
from core.dataClasses import StockTradeDataColumnName
from domain.transaction_data.spider import xueqiu_spider
from domain.transaction_data.repository.impl import cvs_repository


def acquire_stock_list(reacquire=False) -> pd.DataFrame:
    """
    读取或更新当前所有正常上市交易的股票列表csv文件
    :param reacquire:
    :return:
    """
    path = config.get('data', 'folder') + sep + "stock_list.csv"
    try:
        if reacquire:
            raise Exception('to reacquire')
        else:
            df:pd.DataFrame = pd.read_csv(path, dtype = {StockTradeDataColumnName.STOCK_CODE.value : str})
    except:
        df = xueqiu_spider.quoteList()
        df.to_csv(path)
    return df


def get_stock_industres():
    stock_list: pd.DataFrame = acquire_stock_list()
    if stock_list is not None and not stock_list.empty:
        return stock_list["industry"].values.data


def get_stock_list_by_industry(industry):
    stock_list:pd.DataFrame = acquire_stock_list()
    if industry is not None and len(industry) > 0 and stock_list is not None and not stock_list.empty:
        return stock_list[stock_list["industry"] == industry]
    return None


def get_stock_addresses():
    stock_list: pd.DataFrame = acquire_stock_list()
    if stock_list is not None and not stock_list.empty:
        return stock_list["address"].values.data


def get_stock_list_by_industry(address):
    stock_list:pd.DataFrame = acquire_stock_list()
    if address is not None and len(address) > 0 and stock_list is not None and not stock_list.empty:
        return stock_list[stock_list["address"] == address]
    return None